<?php
    namespace app\command;
    
    use think\console\Command;
    use app\client\lib\Redis;
    
    
    use think\console\Input;
    use think\console\input\Argument;
    use think\console\input\Option;
    use think\console\Output;

                                
    class Template extends Command {
        public function __construct() {
            parent::__construct();
        }
        
        protected function configure()
        {
            $this->setName('template');
        }
        
        
        protected function execute(Input $input, Output $output) {
            
            go(function() use($output){
                $redis = new \Swoole\Coroutine\Redis();
                $redis->connect('127.0.0.1', 6379);
                $redis->auth('bill123');
                
                $accessToken = $redis->get('wx_access_token');
                
                
                if(empty($accessToken)){
                    $http = new \Swoole\Coroutine\Http\Client('api.weixin.qq.com', 443, true);
                    $http->get("/cgi-bin/token?grant_type=client_credential&appid=wxde0ae16dacfdfd37&secret=c09417397486c0372c85ecb7a247aaeb");
                    $result = $http->body;
                    if(empty($result)){
                        $redis->close();
                        $http->close();
                    }
                    
                    $result = json_decode($result, true);
                    if(empty($result) || (isset($result['errcode']) && $result['errcode'] != 0)){
                        $redis->close();
                        $http->close();
                    }
                    
                    $accessToken = $result['access_token'];
                    
                    $redis->set('wx_access_token', $accessToken, ['ex' => $result['expires_in'] - 600]);
                }

                
                for($i=0; $i < 100; $i ++){
                    $message = $redis->rPop('order_message_notify');
                    if(empty($message)){
                        break;
                    }
                    
                    $message = json_decode($message, true);
                    
                    $data = [
                        'access_token' => $accessToken,
                        'touser' => $message['openid'],
                        'template_id' => '',
                        'form_id' => $message['form_id'],
                        'data' => [
                            'keyword1' => ['value' => '商家取消'],
                            'keyword2' => ['value' => $message['create_time']],
                            'keyword3' => ['value' => $message['amount']],
                            'keyword4' => ['value' => $message['order_no']],
                        ]
                    ];
                    
                    if($message['status'] == 1){ //确定
                        
                    }
                    
                    $http->post("/cgi-bin/message/wxopen/template/send?access_token={$accessToken}");
                }
                
                $redis->close();
                $http->close();
                
            });
        }
    }